const cloud = require("@cloudbase/node-sdk");

const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV,
});

var db = app.database();
const $ = db.command.aggregate
const _ = db.command

const AION_GMM = db.collection('AION-GMM')

// 获取openId云函数入口函数
exports.main = async (event, context) => {

    // return await AION_ZB_WQ.get()
    var type = event.data.type
    var p_name = event.data.name
    var group_name = event.data.group_name
    var p_job = event.data.p_job
    var p_race = event.data.p_race
    var price = event.data.price
    var min = parseInt(event.data.min)
    var max = parseInt(event.data.max)

    var params = {}

    
    if (group_name)
        params.group_name = group_name
    if (p_job)
        params.p_job = p_job
    if (p_race)
        params.p_race = p_race
    
    if (min || max)
        params.price = _.gte(min).and(_.lte(max))

    if(type=='getList'){
        if(p_name)
            params.p_name = db.RegExp({
                regexp: String(p_name),
                options: 'i', //i表示忽略大小写
            })
        const res = await AION_GMM.where(params)
        const list = (await res
            .skip((event.data.pageIndex - 1) * event.data.pageSize)
            .limit(event.data.pageSize)
            .orderBy("price", price)
            .get()).data

        const total = (await res.count()).total

        return {
            list: list,
            total: total,
        }
    }

    if(type=='getJobCount'){
        // 所有岗位的统计
        const jobCount = await AION_GMM
        .aggregate()
        .sortByCount('$p_job')
        .sort({_id: 1,})
        .end()

        // 所有服务器统计
        const GroupNameCount = await AION_GMM
        .aggregate()
        .sortByCount('$group_name')
        .end()

        return {
            jobCount: jobCount,
            GroupNameCount: GroupNameCount,
        }
    }

    if(type=='getCount'){
        // https://docs.cloudbase.net/api-reference/server/node-sdk/database/database
        
        // 天族=================================================
        // 统计标题包含 百
        const CountBaiT = await AION_GMM
        .aggregate()
        .match({
            group_name: group_name,
            p_race: 1,
            p_name: db.RegExp({regexp: '精百|百夫'})
        })
        .limit(1000)
        .end()
        
        // 统计标题包含 千
        const CountQianT = await AION_GMM
        .aggregate()
        .match({
            group_name: group_name,
            p_race: 1,
            p_name: db.RegExp({regexp: '精千|千夫|老千'})
        })
        .limit(1000)
        .end()
        // 统计标题包含 塔
        const CountTaT = await AION_GMM
        .aggregate()
        .match({
            group_name: group_name,
            p_race: 1,
            p_name: db.RegExp({regexp: '塔'})
        })
        .limit(1000)
        .end()

        // 魔族=================================================
        // 统计标题包含 百
        const CountBaiM = await AION_GMM
        .aggregate()
        .match({
            group_name: group_name,
            p_race: 2,
            p_name: db.RegExp({regexp: '精百|百夫'})
        })
        .limit(1000)
        .end()
        // 统计标题包含 千
        const CountQianM = await AION_GMM
        .aggregate()
        .match({
            group_name: group_name,
            p_race: 2,
            p_name: db.RegExp({regexp: '精千|千夫|老千'})
        })
        .limit(1000)
        .end()
        // 统计标题包含 塔
        const CountTaM = await AION_GMM
        .aggregate()
        .match({
            group_name: group_name,
            p_race: 2,
            p_name: db.RegExp({regexp: '塔'})
        })
        .limit(1000)
        .end()

        let TianModel = {CountBaiT,CountQianT,CountTaT}
        let MoModel = {CountBaiM,CountQianM,CountTaM}

        return {
            TianModel,MoModel
        }
    }
    
};
